/**
 * Created by e430c on 2015/1/14.
 */
var $oilData = [];
var $lon = "";
var $lat = "";
//数据根据距离从小到大排序
function sortOilData(_oilData){
    for(var i = 0;i<_oilData.result.data.length;i++){
        $oilData.push(_oilData.result.data[i]);
    }
    for(var i = 0;i<$oilData.length;i++){
        var $temp = $oilData[i];
        for(var j = i + 1;j<$oilData.length;j++){
            if(parseInt($temp.distance) > parseInt($oilData[j].distance)){
                $oilData[i] = $oilData[j];
                $oilData[j] = $temp;
                $temp = $oilData[i];
            }
        }
    }
}

//将单位米换算成公里
function metreToKilometre(_distance){
    var $distance = parseInt(_distance);
    return (parseInt(($distance / 100) / 10)) + "." + (parseInt(($distance / 100) % 10));
}

function init(){
    var $search = decodeURI(location.search.substring(1));
    $lon = $search.substring($search.indexOf("lon") + 4, ($search.indexOf("&") > 0) ? ($search.indexOf("&")) : ($search.length));
    $search = $search.substring($search.indexOf("&") + 1, $search.length);
    $lat = $search.substring($search.indexOf("lat") + 4, ($search.indexOf("&") > 0) ? ($search.indexOf("&")) : ($search.length));
}

//获取加油站信息
function getOilGas(){
    var url = "api.php";
    $.post(
        url,
        {
            //第三方接口地址
            urls:"http://apis.juhe.cn/oil/local",
            //請求第三方接口方式 post get
            method:'post',
            //code参数为json串,串内参数都需要加密
            code:'',
            //其他无需加密参数,参数为json串,串内参数不加密
            other:'{"lon":"'+$lon+'","lat":"'+$lat+'","key":"8e6aadaad4f9b55eff399221f007130a","format":"2"}',
            keytype: 1
        },
        function (data) {
            var $data = eval("("+data+")");
            if($data.resultcode == 200){
                $("#data").text(data);
                sortOilData($data);
                var dataHTML = "";
                var gastpriceHTML = "";
                for(var i = 0;i<$oilData.length;i++){
                    dataHTML += getDataHtmlModule(i);
                }
                $("#myContainer").append(dataHTML);
                var _i = $oilData.length;
                for(var i = 0;i<_i;i++){
                    gastpriceHTML += getGasHtmlModule(i);
                    $("#gastprice"+$oilData[i].id).append(gastpriceHTML);
                    gastpriceHTML = "";
                }
            } else {
                switch ($data.error_code){
                    case 200701:
                        alert($data.reason,"",1);
                        break;
                    case 200702:
                        alert($data.reason,"",1);
                        break;
                    case 200703:
                        alert($data.reason,"",1);
                        break;
                    case 200705:
                        alert($data.reason,"",1);
                        break;
                }
            }
        }
    );
}

function getDataHtmlModule(_i){
    var $dataHTML = "";
    if(_i % 2 == 0){
        $dataHTML +='<div id="div'+$oilData[_i].id+'" class="row myrow" style="background-color:#ffffff">'+
        '<div class="col-xs-3 mycol-xs-3">'+
        '<div>'+
        '<img src="img/location.png" style="width: 50%;" onclick="map(\''+$oilData[_i].lon+'\',\''+$oilData[_i].lat+'\',\''+$oilData[_i].name+'\',\''+$oilData[_i].address+'\',\''+$oilData[_i].fwlsmc+'\');">'+
        '</div>'+
        '<div style="margin-top: 10px;color: #318ec9;">'+
        '<span>'+metreToKilometre($oilData[_i].distance)+'公里</span>'+
        '</div>'+
        '</div>'+
        '<div class="col-xs-9" style="padding-top: 2.5%;">'+
        '<div class="myspan" style="width: 100%;">'+
        '<span style="font-size: 18px;font-weight: bolder;">'+$oilData[_i].name+'&nbsp;</span>'+
        '</div>'+
        '<div class="myspan" style="width: 100%;margin-top: 1%;">'+
        '<span style="font-size: 13px;color: #a2a2a2">'+$oilData[_i].address+'&nbsp;</span>'+
        '</div>'+
        '<div id="gastprice'+$oilData[_i].id+'" style="margin-top: 3.5%;text-align: center;">'+
        '</div>'+
        '</div>'+
        '</div>';
    } else{
        $dataHTML +='<div id="div'+$oilData[_i].id+'" class="row myrow" style="background-color:#F9F9F9">'+
        '<div class="col-xs-3 mycol-xs-3">'+
        '<div>'+
        '<img src="img/location.png" style="width: 50%;" onclick="map(\''+$oilData[_i].lon+'\',\''+$oilData[_i].lat+'\',\''+$oilData[_i].name+'\',\''+$oilData[_i].address+'\',\''+$oilData[_i].fwlsmc+'\');">'+
        '</div>'+
        '<div style="margin-top: 10px;color: #318ec9;">'+
        '<span>'+metreToKilometre($oilData[_i].distance)+'公里</span>'+
        '</div>'+
        '</div>'+
        '<div class="col-xs-9" style="padding-top: 2.5%;">'+
        '<div class="myspan" style="width: 100%;">'+
        '<span style="font-size: 18px;font-weight: bolder;">'+$oilData[_i].name+'&nbsp;</span>'+
        '</div>'+
        '<div class="myspan" style="width: 100%;margin-top: 1%;">'+
        '<span style="font-size: 13px;color: #a2a2a2">'+$oilData[_i].address+'&nbsp;</span>'+
        '</div>'+
        '<div id="gastprice'+$oilData[_i].id+'" style="margin-top: 3.5%;text-align: center;">'+
        '</div>'+
        '</div>'+
        '</div>';
    }
    return $dataHTML;
}

function getGasHtmlModule(_i){
    var $gastpriceHTML = "";
    for(var j = 0;j<$oilData[_i].gastprice.length;j++){
        if($oilData[_i].gastprice[j].name == "92#"){
            $gastpriceHTML += '<div class="mygas" style="float: left;width: 40%;">'+
            '<div class="my92gas" style="float: left;width: 40%;padding: 2px;">'+
            '<span>92#</span>'+
            '</div>'+
            '<div style="float: right;width: 60%;padding: 2px;">'+
            $oilData[_i].gastprice[j].price+
            '</div>'+
            '<div style="clear: both;"></div>'+
            '</div>';
        } else if($oilData[_i].gastprice[j].name == "95#"){
            $gastpriceHTML += '<div class="mygas" style="float: right;width: 40%;">'+
            '<div class="my95gas" style="float: left;width: 40%;padding: 2px;">'+
            '<span>95#</span>'+
            '</div>'+
            '<div style="float: right;width: 60%;padding: 2px;">'+
            $oilData[_i].gastprice[j].price+
            '</div>'+
            '<div style="clear: both;"></div>'+
            '</div>';
        }
    }
    if($gastpriceHTML == ""){
        $gastpriceHTML += '<div class="mygas" style="float: left;width: 40%;">'+
        '<div class="my92gas" style="float: left;width: 40%;padding: 2px;">'+
        '<span>92#</span>'+
        '</div>'+
        '<div style="float: right;width: 60%;padding: 2px;">'+
        '无'+
        '</div>'+
        '<div style="clear: both;"></div>'+
        '</div>'+
        '<div class="mygas" style="float: right;width: 40%;">'+
        '<div class="my95gas" style="float: left;width: 40%;padding: 2px;">'+
        '<span>95#</span>'+
        '</div>'+
        '<div style="float: right;width: 60%;padding: 2px;">'+
        '无'+
        '</div>'+
        '<div style="clear: both;"></div>'+
        '</div>';
    }
    $gastpriceHTML += '<div style="clear: both"></div>';
    return $gastpriceHTML;
}

//根据加油站地图定位
function map(_lon,_lat,_title,_addr,_desc){
    location.href = "baidumap.html?lon=" + _lon + "&lat=" + _lat + "&title=" + encodeURI(_title) + "&addr=" + encodeURI(_addr) + "&desc=" + encodeURI(_desc);
}